//
//  CommonFunction.m
//  MacStockApp
//
//  Created by 英傑 翁 on 12/5/27.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "CommonFunction.h"
#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h" 
#import "GBTableColumnHeader_position.h"  
#import "GBTableColumnHeader_position_bottom_sumaverage.h"  
#import "GBTableColumnHeader_Indicator.h"  
#import "GBTableColumnHeader_signal.h"  
#import "GBTableColumnHeader_ObjectOtherKey.h"  
#import "GBTableColumnHeader_key.h"
#import "GBall.h"

@implementation CommonFunction

#pragma mark -
#pragma mark 在取得新報價之後進行的報價表資料更新與與歷史資料之最後一筆更新

//物件資料上的兩個更新：報價表資料與歷史資料序列
+(void)after_quotefinished_outarray:(NSMutableArray *)outarray delgeate:(Data_Model *)delgeate {
    
    //取出好幾組的報價資料
    for (NSDictionary *dic in outarray  ) {
        //針對每個股票報價資料
        
        //1.取出物件參考位置    
        Data_Object *obj=[(Data_Model *)delgeate object_data_arrary_object_from_code_datamodel:[dic objectForKey:DB_object_sqlite_basic_code]];
       
        
        //2.針對table_data更新報價
        {
            
            
            //標記：利用欄位屬性定義自動完成程式碼 報價資料取得結束後的資料後匯入動作
            for (NSMutableDictionary *tc in  [[(Data_Model *)delgeate table_column_object_array] table_column_array] ) {
                //取出所有欄位屬性資料
                
                
                    //yahoo bloomberg 共有                
                    if ([[tc objectForKey:Table_Column_source_come_from_Internet_which] isEqualToString:table_column_id_all]) {
                        NSString *sTable_Column_Key =   [tc objectForKey:Table_Column_Key];                
                        
                        //前期
                        if ([[obj table_data] objectForKey:sTable_Column_Key]) {
                           
                            [[obj table_data] removeObjectForKey: [NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                    
                            [[obj table_data] setObject:[[obj table_data] objectForKey:sTable_Column_Key]  forKey:[NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                              
                            
                        }
                                                  
                        
                        //最新
                        [[obj table_data] removeObjectForKey:  sTable_Column_Key];                    
                        [[obj table_data] setObject:[dic objectForKey:sTable_Column_Key]  forKey:sTable_Column_Key]; 
                                                            
                        
                    }
                    //yahoo獨有
                    if ([[tc objectForKey:Table_Column_source_come_from_Internet_which] isEqualToString:table_column_id_Yahoo]) {
                        
                        NSString *sTable_Column_Key =   [tc objectForKey:Table_Column_Key];                
                       
                        //前期
                        if ([[obj table_data] objectForKey:sTable_Column_Key]) {
                            
                            [[obj table_data] removeObjectForKey: [NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                    
                            [[obj table_data] setObject:[[obj table_data] objectForKey:sTable_Column_Key]  forKey:[NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                              
                            
                        }
                        
                        
                        //最新                        
                        [[obj table_data] removeObjectForKey:  sTable_Column_Key];                    
                        [[obj table_data] setObject:[dic objectForKey:sTable_Column_Key]  forKey:sTable_Column_Key];  
                                               
                        
                    } 
                    //bloomberg獨有
                    if ([[tc objectForKey:Table_Column_source_come_from_Internet_which] isEqualToString:table_column_id_Bloomberg]) {
                        
                                    
                        NSString *sTable_Column_Key =   [tc objectForKey:Table_Column_Key];                
                        //前期
                        if ([[obj table_data] objectForKey:sTable_Column_Key]) {
                            
                            [[obj table_data] removeObjectForKey: [NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                    
                            [[obj table_data] setObject:[[obj table_data] objectForKey:sTable_Column_Key]  forKey:[NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                              
                            
                        }
                        
                        
                        //最新                         
                        [[obj table_data] removeObjectForKey:  sTable_Column_Key];                    
                        [[obj table_data] setObject:[dic objectForKey:sTable_Column_Key]  forKey:sTable_Column_Key];   
                                                   
                        
                        
                    } 
                    //google獨有
                    if ([[tc objectForKey:Table_Column_source_come_from_Internet_which] isEqualToString:table_column_id_Google]) {
                        
                        NSString *sTable_Column_Key =   [tc objectForKey:Table_Column_Key];                
                       
                        //前期
                        if ([[obj table_data] objectForKey:sTable_Column_Key]) {
                            
                            [[obj table_data] removeObjectForKey: [NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];                    
                          
                            
                            [[obj table_data] setObject:[[obj table_data] objectForKey:sTable_Column_Key]  forKey:[NSString stringWithFormat:@"Pre_%@", sTable_Column_Key]];
                            
                        }
                        
                        
                        //最新                         
                        [[obj table_data] removeObjectForKey:  sTable_Column_Key];                    
                        [[obj table_data] setObject:[dic objectForKey:sTable_Column_Key]  forKey:sTable_Column_Key];   
                                               
                        
                    
                        
                    }          
                 
                
                
                       
                
            }
            
        }            
         
        
        //3.物件資料內有無歷史資料序列處理
        NSMutableArray *his_data_serial=[obj get_his_data_serial];        
        if (his_data_serial) {
            
            //表示此物件資料已經有歷史資料檔了=>需要針對此歷史資料檔更新最後的報價
            [CommonFunction refresh_quote_last_his_data_update:obj hisdtat_serial:his_data_serial delgeate:delgeate];
            
            
        }
        else {
            // NSLog(@"表示此物件資料無有歷史資料檔了" );
        }
        
        
    }    
}
//歷史資料序列最後一筆與報價資料比較
+(void)refresh_quote_last_his_data_update:(Data_Object *)obj hisdtat_serial:(NSMutableArray *)hisdtat_serial delgeate:(Data_Model *)delgeate{
    
    if (hisdtat_serial) {
        
        //[hisdtat_serial retain];
       
        //有歷史資料序列
        if ([hisdtat_serial count] >= 1   ) {
             
            //1.報價表上的日期與歷史資料序列上最後一筆日期的差值
            int  diff_day_last_quote_his;
            int last_index;            
            {
                
                                
                //日期資料格式
                NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
                [dateFormatters setTimeZone:[NSTimeZone timeZoneWithName: [[obj table_data] objectForKey: DB_object_sqlite_basic_timezone] ]];
                dateFormatters.dateFormat = @"yyyyMMdd";
                
                
                //報價表日期
                NSDate *quote_last_date;
                quote_last_date=[[obj table_data] objectForKey:DB_object_quote_all_datetime];
                
                
                //歷史序列上目前的最後一筆
                NSDate *his_data_last_date;
                
               
                last_index= [hisdtat_serial count]-1 ;
                
                his_data_last_date= [[hisdtat_serial objectAtIndex:last_index] objectForKey:HIS_basic_date] ;  
                
                
                int  timeStamp_quote_last_date = [[dateFormatters stringFromDate:quote_last_date] intValue];                
                int  timeStamp_his_data_last_date = [[dateFormatters stringFromDate:his_data_last_date] intValue];            
                
                
                [dateFormatters release];
                
                
                diff_day_last_quote_his=  timeStamp_quote_last_date-timeStamp_his_data_last_date;  
            }
                      
            
            //2.從報價資料製作最後一個歷史資料節點及設定資料物件的歷史高點與低點
            NSMutableDictionary *dic;            
            float quote_last_open;
            float quote_last_close;
            float quote_last_high;
            float quote_last_low;
            float quote_last_volume;
            
            {
               
                //a.報價表上資料
                NSDate *quote_last_date;
                            
                
                quote_last_date=[[obj table_data] objectForKey:DB_object_quote_all_datetime];
                quote_last_open=[[[obj table_data] objectForKey:DB_object_quote_all_open] floatValue];            
                quote_last_high=[[[obj table_data] objectForKey:DB_object_quote_all_high] floatValue]; 
                quote_last_low=[[[obj table_data] objectForKey:DB_object_quote_all_low] floatValue]; 
                quote_last_close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue]; 
                quote_last_volume=[[[obj table_data] objectForKey:DB_object_quote_all_vol] floatValue];                 
                 
                
                //b.建構節點上最後一筆資料
                dic=[[NSMutableDictionary alloc] init]; 
                
                NSDate *dates=quote_last_date;
                NSNumber *close=[NSNumber numberWithFloat:quote_last_close ];
                NSNumber *open=[NSNumber numberWithFloat:quote_last_open];                               
                NSNumber *volume=[NSNumber numberWithFloat:quote_last_volume];
                NSNumber *high=[NSNumber numberWithFloat:quote_last_high];                
                NSNumber *low=[NSNumber numberWithFloat:quote_last_low];                 
                
                [dic setObject:dates forKey: HIS_basic_date ];
                [dic setObject:close forKey:HIS_basic_close];
                [dic setObject:open forKey:HIS_basic_open];
                [dic setObject:high forKey:HIS_basic_high];
                [dic setObject:low forKey:HIS_basic_low];
                [dic setObject:volume forKey:HIS_basic_vol];
                [dic setObject:close forKey:HIS_basic_adjclose];                  
                
                
                //c.比較取出歷史最大值與最小值
                {
                                        
                    if (quote_last_high>[[obj his_data_high] floatValue] ) {
                        [obj setHis_data_high:[NSNumber numberWithFloat:quote_last_high]];
                    }
                    if (quote_last_low<[[obj his_data_low] floatValue] ) {
                        [obj setHis_data_low:[NSNumber numberWithFloat:quote_last_low]];
                    }         
                }
                 
                
                        
            }
            
            //3.依據時間的差值進行替換或是新增一筆:hisdtat_serial 此時資料含有歷史資料與報價資料整合
            { 
                //NSLog(@"close=%f open=%f high=%f low=%f ",quote_last_close,quote_last_open,quote_last_high,quote_last_low);
                
                //透過收開高低價格是否相同來判斷目前是否正在開盤前狀態
                if (quote_last_close==quote_last_open && quote_last_close==quote_last_high && quote_last_close==quote_last_low) {
                    
                    //NSLog(@"表示可能是在開盤前狀態=%@",[[obj table_data] objectForKey:DB_object_sqlite_basic_code] );
                    [dic release];
                    
                }
                else{
                    //NSLog(@"表示可能 不是在開盤前狀態=%@",[[obj table_data] objectForKey:DB_object_sqlite_basic_code] );
                    
                    if (diff_day_last_quote_his>0) {
                        //NSLog(@"表示報價與歷史最後一筆時間相差一天以上:直接添加");
                        [hisdtat_serial addObject:dic];
                        [dic release];
                        
                        
                        
                    }
                    else if (diff_day_last_quote_his ==0)  {
                        //NSLog(@"表示報價與歷史最後一筆時間相同：要替換 %i %ld",last_index, [hisdtat_serial count]);
                        
                        if (last_index>=0 && last_index<[hisdtat_serial count]) {
                            [hisdtat_serial removeObjectAtIndex:last_index];
                        }
                        
                        [hisdtat_serial addObject:dic];
                        [dic release];
                        
                        
                        
                        
                    }
                    else {
                        // NSLog(@"表示報價與歷史不合理:警告不合理");
                        [dic release];
                    } 
                    
                }                
            }
                       
            //4.將完整歷史資料序列放到物件裡面，有三種存放方式，並針對完整歷史資料序列進行計算
            [obj save_calcuate_his_data_serial: hisdtat_serial reforce_calculate_all:NO delgete_model:delgeate ];
            
                      
        }
        
        //[hisdtat_serial release];
        
    }
    else {
        NSLog(@"refresh_quote_last_his_data_update 無hisdtat_serial");
    }
         
}
  
@end
